import axios from 'axios'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({ showSpinner: false })

const HTTP = axios.create({
  // baseURL: process.env.VUE_APP_BASE_API,
  baseURL: '/api',
  timeout: 240000
})

// 请求拦截
HTTP.interceptors.request.use(config => {
  NProgress.start()
  return config
}, error => {
  return Promise.reject(error)
})

// 响应拦截器
HTTP.interceptors.response.use(res => {
  NProgress.done() // 结束进度条
  return res.data
}, error => {
  if (error.response) {
    // The request was made and the server responded with a status code
    // that falls out of the range of 2xx
    console.error('Response Error:', error.response)
    if (error.response.status === 404) {
      console.error('Resource not found:', error.response.config.url)
    }
  } else if (error.request) {
    // The request was made but no response was received
    console.error('No response received:', error.request)
  } else {
    // Something happened in setting up the request that triggered an Error
    console.error('Error setting up request:', error.message)
  }
  NProgress.done() // 结束进度条
  return Promise.reject(error)
})

export default HTTP
